package com.gab.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gab.mapper.TbUserMapper;
import com.gab.pojo.TbUser;
import com.gab.service.TbUserService;
import com.gab.utils.PageResult;
import  com.gab.pojo.TbUserExample;
import  com.gab.pojo.TbUserExample.Criteria;

@Service
public class TbUserServiceImpl  implements TbUserService{
	@Autowired
	private TbUserMapper tbUserMapper;
	
	public List<TbUser> findAll() {
		// TODO Auto-generated method stub
		return tbUserMapper.selectByExample(null);
	}

	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbUser> page=   (Page<TbUser>) tbUserMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	public void add(TbUser tbUser) {
		tbUserMapper.insert(tbUser);
		
	}

	public void update(TbUser tbUser) {
		tbUserMapper.updateByPrimaryKey(tbUser);
		
	}

	public TbUser findOne(Long id) {
		
		return tbUserMapper.selectByPrimaryKey(id);
	}

	public void delete(Long[] ids) {
		for(Long id:ids){
			tbUserMapper.deleteByPrimaryKey(id);
		}
		
	}

	public PageResult findPage(TbUser tbUser, int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
		
		TbUserExample example=new TbUserExample();
		Criteria criteria = example.createCriteria();
		
		if(tbUser!=null){			
			if(tbUser.getCompanyname()!=null && tbUser.getCompanyname().length()>0){
				criteria.andCompanynameLike("%"+tbUser.getCompanyname()+"%");
			}
			if(tbUser.getMessage()!=null && tbUser.getMessage().length()>0){
				criteria.andMessageLike("%"+tbUser.getMessage()+"%");
			}
			if(tbUser.getName()!=null && tbUser.getName().length()>0){
				criteria.andNameLike("%"+tbUser.getName()+"%");
			}
			if(tbUser.getPhone()!=null && tbUser.getPhone().length()>0){
				criteria.andPhoneLike("%"+tbUser.getPhone()+"%");
			}
			
			if(tbUser.getWechat()!=null && tbUser.getWechat().length()>0){
				criteria.andWechatLike("%"+tbUser.getWechat()+"%");
			}
			
	
		}
		
		Page<TbUser> page= (Page<TbUser>)tbUserMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}

}
